﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.WeChatOfficialAccount.DAL.DAL.Business;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.Models;
using XQ.WeChatOfficialAccount.DAL.Model.Business;
using XQ.WeChatOfficialAccount.BLL.Common;
using XQ.WeChatOfficialAccount.DAL.Model;

namespace XQ.WeChatOfficialAccount.BLL.Business
{
    public class CategoryBLL
    {
        CategoryDal categoryDal = new CategoryDal();

        public bool SaveCategory(Category category, TokenModel token)
        {
            bool isSuccess = false;
            DateTime curTime = DateTime.Now;
            if (category != null && category.id > 0)
            {
                //修改
                category.UpdateTime = curTime;
                category.UpdateUserID = token.UID;
                isSuccess = categoryDal.UpdateCategory(category) > 0;

            }
            else
            {
                //新增
                category.AddTime = curTime;
                category.AddUserID = token.UID;

                isSuccess = categoryDal.AddModel(category) != null;
            }

            return isSuccess;
        }

        /// <summary>
        /// 根据条件获取列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public List<Category> GetList(CategorySearchModel searchModel, Pagination page, TokenModel token)
        {
            return categoryDal.GetList(searchModel,page, token);
        }

        public List<CategoryModel> GetTree(TokenModel token)
        {
            List<CategoryModel> categoryModels = new List<CategoryModel>();
            List<Category> categorys = categoryDal.FindAllModel(c => c.ParentID==0 || c.ParentID == null);
            foreach (Category category in categorys)
            {
                categoryModels.Add(ClassHelper.ParentCopyToChild<Category, CategoryModel>(category));
            }
            //加载子节点
            foreach (CategoryModel newCategory in categoryModels)
            {
                GetChildNode(newCategory);
            }
            return categoryModels;
        }

        private void GetChildNode(CategoryModel categoryModel)
        {
            List<CategoryModel> categoryModels = new List<CategoryModel>();
            List<Category> categorys = categoryDal.FindAllModel(c => c.ParentID == categoryModel.id);
            foreach (Category category in categorys)
            {
                categoryModels.Add(ClassHelper.ParentCopyToChild<Category, CategoryModel>(category));
            }
            categoryModel.Children = categoryModels;
            //递规加载子节点
            foreach (CategoryModel newCategory in categoryModels)
            {
                GetChildNode(newCategory);
            }
        }


        public Category GetDetailByID(int categoryID)
        {
            return categoryDal.FindModel(c => c.id == categoryID);
        }

        /// <summary>
        /// 获取类别详情
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public bool DeleteByIds(List<int> ids)
        {
            return categoryDal.DeleteModel(ph => ids.Contains(ph.id)) == ids.Count;
        }
    }
}
